home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 September / PCWorld_2007-09_cd.bin / system / ntfs / ntfsundelete.exe / {app} / atexit.pyc (.txt) next >
Python Compiled Bytecode  |  2007-02-05  |  2KB  |  77 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''
  5. atexit.py - allow programmer to define multiple exit functions to be executed
  6. upon normal program termination.
  7.  
  8. One public function, register, is defined.
  9. '''
  10. __all__ = [
  11.     'register']
  12. _exithandlers = []
  13.  
  14. def _run_exitfuncs():
  15.     '''run any registered exit functions
  16.  
  17.     _exithandlers is traversed in reverse order so functions are executed
  18.     last in, first out.
  19.     '''
  20.     exc_info = None
  21.     while _exithandlers:
  22.         (func, targs, kargs) = _exithandlers.pop()
  23.         
  24.         try:
  25.             func(*targs, **kargs)
  26.         continue
  27.         except SystemExit:
  28.             exc_info = sys.exc_info()
  29.             continue
  30.             import sys as sys
  31.             import traceback as traceback
  32.             print >>sys.stderr, 'Error in atexit._run_exitfuncs:'
  33.             traceback.print_exc()
  34.             exc_info = sys.exc_info()
  35.             continue
  36.         
  37.  
  38.         None<EXCEPTION MATCH>SystemExit
  39.     if exc_info is not None:
  40.         raise exc_info[0], exc_info[1], exc_info[2]
  41.     
  42.  
  43.  
  44. def register(func, *targs, **kargs):
  45.     '''register a function to be executed upon normal program termination
  46.  
  47.     func - function to be called at exit
  48.     targs - optional arguments to pass to func
  49.     kargs - optional keyword arguments to pass to func
  50.     '''
  51.     _exithandlers.append((func, targs, kargs))
  52.  
  53. import sys
  54. if hasattr(sys, 'exitfunc'):
  55.     register(sys.exitfunc)
  56.  
  57. sys.exitfunc = _run_exitfuncs
  58. del sys
  59. if __name__ == '__main__':
  60.     
  61.     def x1():
  62.         print 'running x1'
  63.  
  64.     
  65.     def x2(n):
  66.         print 'running x2(%r)' % (n,)
  67.  
  68.     
  69.     def x3(n, kwd = None):
  70.         print 'running x3(%r, kwd=%r)' % (n, kwd)
  71.  
  72.     register(x1)
  73.     register(x2, 12)
  74.     register(x3, 5, 'bar')
  75.     register(x3, 'no kwd args')
  76.  
  77.